﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

namespace RioStudio.Website
{
    public class RioPage : Page
    {
        public string ViewStateFilePath
        {
            get
            {
                if (Session["viewstateFilPath"] == null)
                {
                    string folderName = Path.Combine(Request.PhysicalApplicationPath, "ViewState");

                    if (!Directory.Exists(folderName))
                    {
                        Directory.CreateDirectory(folderName);
                    }

                    string fileName = Session.SessionID + "-" +
                           Path.GetFileNameWithoutExtension(Request.Path).Replace("/", "-") + ".vs";
                    string filepath = Path.Combine(folderName, fileName);

                    //if (!File.Exists(filepath))
                    //{
                    //    File.Create(filepath);
                    //}

                    Session["viewstateFilPath"] = filepath;
                }
                return Session["viewstateFilPath"].ToString();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            var config = WebConfigurationManager.AppSettings;

            var keyword = new HtmlMeta();
            keyword.Name = "keywords";
            if (!string.IsNullOrEmpty(config["keyword"]))
            {
                keyword.Content = config["keyword"];
            }

            Page.Header.Controls.Add(keyword);

            var description = new HtmlMeta();
            description.Name = "description";
            if (!string.IsNullOrEmpty(config["description"]))
            {
                description.Content = config["description"];
            }

            Page.Header.Controls.Add(description);            

            base.OnInit(e);
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            if (!File.Exists(ViewStateFilePath))
                return null;
            else
            {
                // open the file
                StreamReader sr = File.OpenText(ViewStateFilePath);
                string viewStateString = sr.ReadToEnd();
                sr.Close();

                // deserialize the string
                LosFormatter los = new LosFormatter();
                return los.Deserialize(viewStateString);
            }
        }

        protected override void SavePageStateToPersistenceMedium(object state)
        {
            LosFormatter los = new LosFormatter();
            StringWriter sw = new StringWriter();
            los.Serialize(sw, state);

            StreamWriter w = File.CreateText(ViewStateFilePath);
            w.Write(sw.ToString());
            w.Flush();
            w.Close();
            sw.Close();
        }
    }
}